﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Alti.SharePoint.SPDataGenerator.Views;
using Alti.SharePoint.SPDataGenerator.Models;
using Alti.SharePoint.SPDataGenerator.Presenters;
using System.Diagnostics;

namespace Alti.SharePoint.SPDataGenerator
{
    public partial class frmMain : Form, ISPDataGeneratorView
    {
        private SPDataGeneratorPresenter presenter;
        public frmMain()
        {
            InitializeComponent();

            presenter = new SPDataGeneratorPresenter(this);
        }

        private void btnConnectSharePoint_Click(object sender, EventArgs e)
        {
            presenter.LoadSiteDataAsync();
        }


        #region ISPDataGeneratorView Members

        public string DestinationNamespace
        {
            get
            {
                if (string.IsNullOrEmpty(tbNamespace.Text))
                    return string.Empty;
                return tbNamespace.Text;
            }
        }

        public List<SPDataContentType> GetContentTypesToInclude()
        {
            if (this.cblContenttypes.SelectedItems == null)
                return null;
            List<SPDataContentType> listToReturn = new List<SPDataContentType>();
            foreach (object item in cblContenttypes.CheckedItems)
            {
                listToReturn.Add((item as SPDataContentType));
            }
            return listToReturn;
        }

        public List<SPDataList> GetListsToInclude()
        {
            if (this.cblLists.SelectedItems == null)
                return null;
            List<SPDataList> listToReturn = new List<SPDataList>();
            foreach (object item in cblLists.CheckedItems)
            {
                listToReturn.Add((item as SPDataList));
            }
            return listToReturn;
        }


        public void WriteOutputSPData(string SPDataContent)
        {
            Clipboard.SetText(SPDataContent);
            MessageBox.Show("SPData set in clipboard !", "SPData Generated", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void ShowError(Exception exp)
        {
            MessageBox.Show(exp.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void BindWebContentTypes(List<SPDataContentType> cts)
        {
            this.cblContenttypes.Items.Clear();
            cts.ForEach(item => cblContenttypes.Items.Add(item));
        }

        public void BindWebLists(List<SPDataList> lists)
        {
            this.cblLists.Items.Clear();
            lists.ForEach(item => cblLists.Items.Add(item));
        }

        #endregion


        private void btnGenerate_Click(object sender, EventArgs e)
        {
            presenter.GenerateSPDataClass();
        }


        public string WebUrl
        {
            get { return tbWebUrl.Text; }
        }

        private void cbSelectAllLists_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < cblLists.Items.Count; i++)
                cblLists.SetItemChecked(i, cbSelectAllLists.Checked);
        }

        private void cbSelectAllCT_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < cblContenttypes.Items.Count; i++)
                cblContenttypes.SetItemChecked(i, cbSelectAllCT.Checked);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "http://www.alti.fr";
                p.Start();
            }
            catch (Exception)
            { }
        }





        public bool IsSiteDataLoaded
        {
            set
            {
                PanelLists.Enabled = value;
                PanelOptions.Enabled = value;
            }
        }


        public bool IsSiteDataLoading
        {
            set
            {
                progressBarLoading.Visible = value;
            }
        }




        public bool IsHiddenFieldInclude
        {
            get { return cbGenerateHiddenFields.Checked; }
        }
    }
}
